from django.db import models

from django.db import models


class User(models.Model):
    """管理员，教师"""
    username = models.CharField(max_length=150, unique=True, db_comment="用户名")
    password = models.CharField(max_length=128)
    nickname = models.CharField(blank=True, null=True, max_length=150)
    email = models.EmailField(blank=True, null=True, unique=True)
    tel = models.CharField(blank=True, null=True, max_length=11)
    is_active = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)
    is_teacher = models.BooleanField(default=False, blank=True, null=True, db_comment="是否是教师")
    status = models.BooleanField(default=False, db_comment="是否启用")
    created_time = models.DateTimeField(auto_now_add=True)
    is_delete = models.BooleanField(default=False)

    class Meta:
        db_table = "tb_user"
        verbose_name = "用户"
        verbose_name_plural = "用户"


class Role(models.Model):
    name = models.CharField(max_length=150)
    code = models.CharField(blank=True, null=True, max_length=50, unique=True)
    description = models.TextField()
    created_time = models.DateTimeField(auto_now_add=True)
    status = models.BooleanField(default=True, db_comment="是否启用")

    class Meta:
        db_table = "tb_role"
        verbose_name = "角色"
        verbose_name_plural = "角色"


class UserRole(models.Model):
    """用户-角色关联表（多对多）"""
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="user_roles", db_comment="关联用户")
    role = models.ForeignKey(Role, on_delete=models.CASCADE, related_name="role_users", db_comment="关联角色")
    created_time = models.DateTimeField(auto_now_add=True, db_comment="关联时间")

    class Meta:
        db_table = "tb_user_role"
        verbose_name = "用户角色关联"
        verbose_name_plural = "用户角色关联"
        unique_together = ("user", "role")  # 避免重复关联


class Class(models.Model):
    name = models.CharField(max_length=150)
    code = models.CharField(blank=True, null=True, max_length=50, unique=True)
    created_time = models.DateTimeField(auto_now_add=True)
    teacher = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        db_table = "tb_class"
        verbose_name = "班级"
        verbose_name_plural = "班级"


class Student(models.Model):
    name = models.CharField(max_length=150)
    email = models.EmailField(unique=True)
    password = models.CharField(max_length=128)
    status = models.BooleanField(default=False, db_comment="是否激活")
    tel = models.CharField(blank=True, null=True, max_length=11)
    created_time = models.DateTimeField(auto_now_add=True)
    class_id = models.ForeignKey(Class, on_delete=models.CASCADE)
    is_staff = models.BooleanField(default=False)
    class Meta:
        db_table = "tb_student"
        verbose_name = "学生"
        verbose_name_plural = "学生"


class Group(models.Model):
    name = models.CharField(max_length=150)
    student_id = models.ForeignKey(Student, on_delete=models.CASCADE)
    class_name = models.ForeignKey(Class, on_delete=models.CASCADE, related_name="groups", db_comment="所属班级")

    class Meta:
        db_table = "tb_group"
        verbose_name = "分组"
        verbose_name_plural = "分组"


class GroupMember(models.Model):
    """分组-学生关联表（多对多）"""
    group = models.ForeignKey(Group, on_delete=models.CASCADE, related_name="members", db_comment="关联分组")
    student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name="joined_groups", db_comment="关联学生")
    joined_time = models.DateTimeField(auto_now_add=True, db_comment="加入时间")

    class Meta:
        db_table = "tb_group_member"
        verbose_name = "分组成员"
        verbose_name_plural = "分组成员"
        unique_together = ("group", "student")  # 避免重复加入